package com.estar.transmit.controller;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.estar.companyurl.entity.TmblCompanyUrlEntity;
import com.estar.transmit.vo.RequestVO;
import com.estar.transmit.vo.ReturnVO;
import net.sf.json.JSONObject;

@Controller("sendSurvey")
@Scope("prototype")
public class SendSurvey {
	
	private static final Logger logger = Logger.getLogger(SendSurvey.class);

	@Autowired
	private SystemService systemService;
	@Autowired
	public GetSurveyList getSurveyList;
	
	private java.text.DateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@RequestMapping(params = "companyInfo")
	public void companyInfo(HttpServletRequest request,HttpServletResponse response){
		
		RequestVO requestVo=new RequestVO();
		ReturnVO returnVo=new ReturnVO();
		
		InputStreamReader isr = null;
		BufferedReader in =null;
		
		//logger.info("调用接口于"+df.format(new Date()));
		try {
			isr = new InputStreamReader(request.getInputStream(), "UTF-8");
			in = new BufferedReader(isr);
			StringBuffer inStr = new StringBuffer("");
			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				inStr.append(inputLine);
				inStr.append("\n");
			}
			in.close();
			
			if (inStr == null || "".equals(new String(inStr))) {
				returnVo.setUesrNme("接收的数据为空");
				returnVo.setResultCde("0");
				returnVo.setMessage("接收的数据为空");
			} else {
				System.out.println(inStr);
				JSONObject jsonObject = JSONObject.fromObject(inStr.toString());
				 requestVo=(RequestVO) JSONObject.toBean(jsonObject,RequestVO.class);//接收json数据，并且变成vo类

				 //判断用户
				 String hql="from TmblCompanyUrlEntity where uesrnme=? and passwd=?";
				 List<TmblCompanyUrlEntity > company=this.systemService.findHql(hql, requestVo.getUesrNme(),requestVo.getPassWd());
				 
				 logger.info(requestVo.getUesrNme()+"调用接口于"+df.format(new Date()));
				 
				 if(company.size()>0 && company!=null){
					 //寻找数据，传输数据
					 	Date startdate=df.parse(requestVo.getStartDate());
					 	Date enddate=df.parse(requestVo.getEndDate());
					 	String name=requestVo.getUesrNme();
					    returnVo=getSurveyList.getSurvey(requestVo,startdate,enddate,name);
					    
				 }else{
					 	returnVo.setUesrNme(requestVo.getUesrNme());
						returnVo.setResultCde("0");
						returnVo.setMessage("用户名或者密码错误");
				 }
				 
			}
				
			
		}catch(ParseException e){
			returnVo.setUesrNme("时间格式不正确");
			returnVo.setResultCde("0");
			returnVo.setMessage("时间格式不正确");
		}catch (Exception e) {
			logger.error("接口系统异常：",e);
			returnVo.setUesrNme("解析出现异常");
			returnVo.setResultCde("0");
			returnVo.setMessage("解析出现异常");
		} finally {		// 返回数据
			String outStr = JSONObject.fromObject(returnVo).toString();
			logger.info("调用接口于"+df.format(new Date())+";内容============"+outStr);
			try {
				if(in!=null){
					in.close();
				}
				if(isr!=null){
					isr.close();
				}
				
				response.setContentType("text/html; charset=GB2312");
				PrintWriter write = response.getWriter();
				write.write(outStr);
				write.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}
}
